package com.frame.demo.rest;

import com.frame.demo.rest.sign.Signer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.outer.pack.ServerConfig;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author yuefeng
 * @version 3.3.1
 * @date 15/12/22
 */
public class RestClient extends Signer {

    private static final String OS_CODE = "2";
    private Retrofit mRetrofit;

    public RestClient() {
        Gson gson = new GsonBuilder()
                .registerTypeAdapterFactory(new ItemTypeAdapterFactory())
                .create();

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();

                        return chain.proceed(request);
                    }
                })
                .build();



        mRetrofit = new Retrofit.Builder()
                .baseUrl(ServerConfig.getServerHOST())
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

    }

    public Retrofit getRestAdapter() {
        return mRetrofit;
    }
}
